﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class ExamInGroup : System.Web.UI.Page
    {
        static int examgroupid = 0;
        static int examid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (examgroupid != 0)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myExamGroup = from eg in db.ExamGroup
                                      where eg.ExamGroupID == examgroupid
                                      select eg.GroupName;
                    foreach (var eg in myExamGroup)
                    {
                        DropDownField1.Text = eg.ToString();
                    }
                    var myEIG = from c in db.ExamInGroup
                                where c.ExamGroupID == examgroupid
                                select new { ExamName = c.ExamInfo.ExamName, ExamID = c.ExamInfo.ExamID };
                    this.Store2.DataSource = myEIG;
                    this.Store2.DataBind();
                }
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                examgroupid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myExamGroup = from eg in db.ExamGroup
                                  where eg.ExamGroupID == examgroupid
                                  select eg.GroupName;
                foreach (var eg in myExamGroup)
                {
                    DropDownField1.Text = eg.ToString();
                }
                var myEIG = from c in db.ExamInGroup
                            where c.ExamGroupID == examgroupid
                            select new { ExamName = c.ExamInfo.ExamName, ExamID = c.ExamInfo.ExamID };
                this.Store2.DataSource = myEIG;
                this.Store2.DataBind();
            }
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (examgroupid == 0)
            {
                X.Msg.Alert("提示信息", "请选择一个组").Show();
            }
            else
            {
                examid = 0;
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Show();
            }
        }
 
        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newexam = from c in db.ExamInGroup
                                    where c.ExamID == examid && c.ExamGroupID==examgroupid
                                    select c;
                    ExamAnalysis.Model.ExamInGroup newe = newexam.First();
                    db.ExamInGroup.DeleteOnSubmit(newe);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("ExamInGroup.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个考试").Show();
            }
        }

        //protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        examid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myExam = from c in db.ExamInfo
        //                      where c.ExamID==examid
        //                      select c.ExamName;
        //        foreach (var exam in myExam)
        //        {
        //            DropDownField2.Text = exam.ToString();
        //        }
        //    }
        //}

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //if (DropDownField2.Text.ToString().Equals("请选择"))
            //{
            //    X.Msg.Alert("提示信息", String.Format("请选择一个考试")).Show();
            //}
            //else
            //{

                RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examid = Convert.ToInt32(row.RecordID);
                }
                ExamAnalysis.Model.ExamInGroup neweig = new ExamAnalysis.Model.ExamInGroup();
                neweig.ExamID = examid;
                neweig.ExamGroupID = examgroupid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var theeig = from eig in db.ExamInGroup
                                 where eig.ExamID == neweig.ExamID && eig.ExamGroupID == neweig.ExamGroupID
                                 select eig;
                    if (theeig.Count() == 0)
                    {
                        db.ExamInGroup.InsertOnSubmit(neweig);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功添加考试")).Show();
                        examid = 0;
                        Response.Redirect("ExamInGroup.aspx");
                    }
                    else
                    {
                        X.Msg.Alert("提示信息", String.Format("该考试已在此组中")).Show();
                    }
                }
                
           // }
        }
    }
}